package org.sqlproc.sample.simple;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sqlproc.engine.SqlEngine;
import org.sqlproc.engine.SqlEngineLoader;
import org.sqlproc.engine.SqlOrder;
import org.sqlproc.engine.SqlPropertiesLoader;
import org.sqlproc.sample.simple.model.Person;

public class Main {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    private SessionFactory sessionFactory;
    private SqlEngineLoader sqlLoader;

    public Main() {
        Configuration configuration = new Configuration().configure("hibernate.cfg.xml");
        sessionFactory = configuration.buildSessionFactory();
        SqlPropertiesLoader loader = new SqlPropertiesLoader("queries.properties", this.getClass());
        sqlLoader = new SqlEngineLoader(loader.getProperties());
    }

    public void setupDb() {
        Person p;
        Session session = null;
        p = new Person("Jan");
        try {
            session = sessionFactory.openSession();
            p = new Person("Jan");
            session.save(p);
            p = new Person("Janik");
            session.save(p);
            p = new Person("Honza");
            session.save(p);
            p = new Person("Honzik");
            session.save(p);
            p = new Person("Andrej");
            session.save(p);
        } finally {
            if (session != null)
                session.close();
        }
    }

    public List<Person> listAll() {
        SqlEngine sqlEngine = sqlLoader.getSqlEngine("ALL");
        Session session = null;
        try {
            session = sessionFactory.openSession();
            List<Person> list = sqlEngine.query(session, Person.class, null, SqlEngine.ASC_ORDER);
            logger.info("listAll size: " + list.size());
            return list;
        } finally {
            if (session != null)
                session.close();
        }
    }

    public List<Person> listSome(Person person) {
        SqlEngine sqlEngine = sqlLoader.getSqlEngine("ALL");
        Session session = null;
        try {
            session = sessionFactory.openSession();
            List<Person> list = sqlEngine.query(session, Person.class, person, SqlOrder.getDescOrder(2));
            logger.info("listSome size: " + list.size());
            return list;
        } finally {
            if (session != null)
                session.close();
        }
    }

    public List<Person> listLike(Person person) {
        SqlEngine sqlEngine = sqlLoader.getSqlEngine("LIKE");
        Session session = null;
        try {
            session = sessionFactory.openSession();
            List<Person> list = sqlEngine.query(session, Person.class, person, SqlOrder.getDescOrder(2));
            logger.info("listSome size: " + list.size());
            return list;
        } finally {
            if (session != null)
                session.close();
        }
    }

    public static void main(String[] args) {
        Main main = new Main();
        main.setupDb();
        main.listAll();
        Person person = new Person();
        person.setName("Jan");
        main.listSome(person);
        person.setName("an");
        main.listLike(person);
    }
}
